import { fileURLToPath, URL } from 'node:url'
import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import eslintPlugin from 'vite-plugin-eslint'
import stylelint from 'vite-plugin-stylelint'
import { resolve } from 'path'
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
// import { createHtmlPlugin } from 'vite-plugin-html'
import legacyPlugin from '@vitejs/plugin-legacy'


// const createInjectScript = (mode: string) => {
// 	if (loadEnv(mode, process.cwd()).VITE_APP_IS_WEB === 'true') {
// 		return '<script src="fxScript.js"></script>'
// 	} else {
// 		return '<script src="cordova.js"></script>'
// 	}
// }
const createBaseUrl = (mode: string) => {
	if (loadEnv(mode, process.cwd()).VITE_APP_IS_WEB === 'true') {
		return '/'
	} else {
		return './'
	}
}
// https://vitejs.dev/config/
export default ({ mode }: { mode: string }) => {
	return defineConfig({
		base: createBaseUrl(mode),
		plugins: [
			vue(),
			vueJsx(),
			eslintPlugin({
				cache: false
			}),
			stylelint({
				fix: true,
				cache: false
			}),
			createSvgIconsPlugin({
				iconDirs: [resolve(process.cwd(), 'src/assets/images/icon/svg')],
				symbolId: 'fx-[name]'
			}),
			// createHtmlPlugin({
			// 	inject: {
			// 		data: {
			// 			injectScript: createInjectScript(mode)
			// 		}
			// 	}
			// }),
			legacyPlugin({
				targets: ['chrome 52']
			})
		],
		css: {
			preprocessorOptions: {
				less: {
					modifyVars: {
						'primary-color': '#5D91FB'
					},
					javascriptEnabled: true
				}
			}
		},
		resolve: {
			alias: {
				'@': fileURLToPath(new URL('./src', import.meta.url)),
				$assets: fileURLToPath(new URL('./src/assets', import.meta.url)),
				$images: fileURLToPath(new URL('src/assets/images', import.meta.url))
			}
		},
		build: {
			sourcemap: true
		},
		server: {
			port: 8082,
			host: true,
			open: true,
			proxy: {
				// https://cn.vitejs.dev/config/#server-proxy
				'/dev-api': {
					// target: 'https://5511dj8yk636.vicp.fun',
					target: 'http://47.122.64.230:18080',
					changeOrigin: true,
					rewrite: (p) => p.replace(/^\/dev-api/, '')
				},
				'/prod-api': {
					// target: 'https://5511dj8yk636.vicp.fun',
					target: 'http://47.122.64.230:18080',
					changeOrigin: true,
					rewrite: (p) => p.replace(/^\/prod-api/, '')
				}
			}
		}
	})
}
